﻿using System;
using System.IO;
using System.Windows.Media.Imaging;

namespace SoGame.Helper
{
    /// <summary>
    /// Class with usefull methods to deals with types
    /// </summary>
    public static class ConvertersHelper
    {
        /// <summary>
        /// Converts a stream into byte array
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToByteArray(Stream stream)
        {
            long originalPosition = stream.Position;
            stream.Position = 0;

            try
            {
                byte[] readBuffer = new byte[4096];

                int totalBytesRead = 0;
                int bytesRead;

                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;

                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }

                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                stream.Position = originalPosition;
            }
        }

        /// <summary>
        ///  Convert a soure into a byte array
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static byte[] ConvertToBytes(BitmapSource source)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(ms);

                return ms.ToArray();
            }
        }

        /// <summary>
        /// Converts a bytearray into a bitmapImage
        /// </summary>
        /// <param name="byteArrayIn"></param>
        /// <returns></returns>
        public static BitmapImage ByteArrayToImage(byte[] byteArrayIn)
        {
            using (MemoryStream ms = new MemoryStream(byteArrayIn))
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.StreamSource = ms;
                return bitmap;
            }
        }

        /// <summary>
        /// Converts a bitmapSource into a bitmap Image
        /// </summary>
        /// <param name="bitmapsource"></param>
        /// <returns></returns>
        public static BitmapImage BitmapSourceToBitmapImage(BitmapSource bitmapsource)
        {
            BitmapImage bitmap = null;
            using (MemoryStream ms = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapsource));
                encoder.Save(ms);
                bitmap = new BitmapImage();
                bitmap.StreamSource = ms;
            }
            return bitmap;
        }
    }
}